package com.nutrition.health.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.util.StdConverter;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Entity
@Table(name = "user_health_profiles")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString(exclude = {"user"})
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"user"}, allowSetters = true)
public class UserHealthProfile {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(name = "user_id", nullable = false)
    private Long userId;

    @NotNull
    private LocalDate dateOfBirth;

    @NotNull
    @Enumerated(EnumType.STRING)
    private Gender gender;

    @NotNull
    @Min(1)
    private Double height; // 身高（厘米）

    @NotNull
    @Min(1)
    private Double weight; // 体重（公斤）

    @Min(1)
    private Double targetWeight; // 目标体重（公斤）

    @Enumerated(EnumType.STRING)
    private ActivityLevel activityLevel; // 活动水平

    @Enumerated(EnumType.STRING)
    private Goal goal; // 健康目标

    @Column(length = 1000)
    @JsonDeserialize(converter = ListToStringConverter.class)
    private String healthConditions; // 健康状况，如慢性病、过敏等

    @Column(length = 1000)
    @JsonDeserialize(converter = ListToStringConverter.class)
    private String dietaryRestrictions; // 饮食限制，如素食主义、无麸质等

    @CreatedDate
    @Column(nullable = false, updatable = false)
    private LocalDateTime createdAt;

    @LastModifiedDate
    @Column(nullable = false)
    private LocalDateTime updatedAt;

    // 性别枚举
    public enum Gender {
        MALE, FEMALE, OTHER
    }

    // 活动水平枚举
    public enum ActivityLevel {
        SEDENTARY, // 久坐不动
        LIGHTLY_ACTIVE, // 轻度活动
        MODERATELY_ACTIVE, // 中度活动
        VERY_ACTIVE, // 高度活动
        EXTREMELY_ACTIVE // 极度活动
    }

    // 健康目标枚举
    public enum Goal {
        LOSE_WEIGHT, // 减肥
        MAINTAIN_WEIGHT, // 保持体重
        GAIN_WEIGHT, // 增重
        BUILD_MUSCLE, // 增肌
        IMPROVE_HEALTH // 改善健康
    }

    // 自定义转换器：将List转换为逗号分隔的字符串
    public static class ListToStringConverter extends StdConverter<Object, String> {
        @Override
        public String convert(Object value) {
            if (value == null) {
                return "";
            }

            if (value instanceof List) {
                List<?> list = (List<?>) value;
                return list.stream()
                        .map(Object::toString)
                        .collect(Collectors.joining(", "));
            } else if (value instanceof String) {
                return (String) value;
            }

            return value.toString();
        }
    }
}